package com.vcredit.creditcat.web;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.vcredit.creditcat.framework.util.HttpClientUtil;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.vo.WalletDrawLogVo;
import com.vcredit.framework.util.JacksonUtil;

@Controller
@RequestMapping("business/wallet")
public class WalletController {
	
	@Value("${business.wallet.drawHandlerUrl}")
	private String  walletDrawHandlerUrl;
	
	@Value("${business.wallet.drawLogQueryUrl}")
	private String  walletDrawLogQueryUrl;
	
	@Value("${business.wallet.drawHandlerFilePath}")
	private String walletFilePath;
	
	@Value("${business.wallet.drawStatusUpdateUrl}")
	private String walletDrawStatusUpdateUrl;

	@RequestMapping("page")
	public String toWalletDrawPage() {
		return "business/walletdraw";
	}
	
	@RequestMapping("upload")
	@ResponseBody
	public Map<String,Object> uploadFile(@RequestParam(value = "file") MultipartFile file) {
		try {
			Date date = new Date();
			String dateStr = ConcurrentDateUtil.formatVirguleDate(date);
			String dateTimeStr = ConcurrentDateUtil.formatHHMMSS(date);
			File baseFile = new File(walletFilePath + dateStr);  
	        if(!baseFile.exists()){  
	        	baseFile.mkdirs();  
	        } 
	        File targetFile = new File(baseFile,dateTimeStr + ".txt");
			// 上传文件
			file.transferTo(targetFile);  
			Map<String,String> param = new HashMap<String,String>();
			param.put("filePath", targetFile.getPath());
			String resultStr = HttpClientUtil.doPostByJson(walletDrawHandlerUrl, param);
			Map<String,Object> result = JacksonUtil.Json2Map(resultStr);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 条件查询提现记录
	 * @author zhaoqi01
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("query")
	public Map<String, Object> query(@RequestBody WalletDrawLogVo param) {
		try {
			String json = HttpClientUtil.doPostByJson(walletDrawLogQueryUrl, param);
			Map<String, Object> result = JacksonUtil.Json2Map(json);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更新提现状态
	 * @author zhaoqi01
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("drawstatus/update")
	public Map<String, Object> update(@RequestBody WalletDrawLogVo param) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("status", "000");
		try {
			String json = HttpClientUtil.doPostByJson(walletDrawStatusUpdateUrl, param);
			Map<String, Object> result = JacksonUtil.Json2Map(json);
			if(!("000".equals(result.get("status")) && (Boolean)result.get("data")))
				map.put("status", "001");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
